<?php
/**
 * 用户身份返利金额
 * @param $identity_type
 * @param $data
 */
function identity_type_bonus($identity_type, $data){
    //1普通成员  2初级专员 3中级专员 4高级专员
    switch ($identity_type) {
        case \app\common\model\Users::PRIMARY_USER:
            $bonus = $data['bonus_three'];
            break;
        case \app\common\model\Users::INTERMEDIATE_USER :
            $bonus = $data['bonus_two'];
            break;
        case \app\common\model\Users::SENIOR_USER :
            $bonus = $data['bonus_one'];
            break;
        default :
            $bonus = 0;
            break;
    }
    return $bonus;
}

/**
 * 计算上级返利金额
 * @param $user_data        当前用户信息
 * @param $product_data     产品信息
 * @return array

 */
function get_parent_bonus($user_data = [], $product_data = []){
    $bonus_data = ['agent_one_bonus'=>0,'agent_two_bonus'=>0];

    if($user_data == false || $product_data == false) return $bonus_data;

    //获取与用户关联的用户信息，并且计算其身份所对应的返利金额
    $parent_user = \app\common\model\Users::where(['uid'=>[$user_data['uid'],$user_data['parent_id'],$user_data['second_id']]])->column('uid,identity_type,spread_code');
    foreach($parent_user as &$val){
        $val['bonus'] = identity_type_bonus($val['identity_type'],$product_data);
    }
    //计算一级返利金额
    if($user_data['parent_id'] > 0 && $user_data['identity_type'] < $parent_user[$user_data['parent_id']]['identity_type']){
        $bonus_data['agent_one_bonus'] = round(($parent_user[$user_data['parent_id']]['bonus'] - $parent_user[$user_data['uid']]['bonus'])/2 , 2);
    }
    //计算二级返利金额
    if($user_data['second_id'] > 0 && $parent_user[$user_data['parent_id']]['identity_type'] < $parent_user[$user_data['second_id']]['identity_type']){
        $bonus_data['agent_two_bonus'] = round(($parent_user[$user_data['second_id']]['bonus'] - $parent_user[$user_data['uid']]['bonus'])/2 , 2);
    }
    return $bonus_data;
}

/**
 * 验证银行
 */
function check_bank($cardNo){
    $url = 'https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?cardNo='.$cardNo.'&cardBinCheck=true';
    $ch  = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//检查服务器SSL证书
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $res = curl_exec($ch);
    curl_close($ch);

    return json_decode($res, true);
}


